package com.ruoyi.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.EApiLog;
import com.ruoyi.system.entity.BusinessInfo;
import com.ruoyi.system.entity.BusinessShareholder;
import com.ruoyi.system.entity.SocialInfo;
import com.ruoyi.system.service.IEApiLogService;
import com.ruoyi.system.service.QiXinBaoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:启信宝接口
 * @FileName: QiXinBaoService
 * @Author: 张琳杰
 * @Date: 2022年03月23日 16:51
 */
@Service
public class QiXinBaoServiceImpl implements QiXinBaoService {
    private static final Logger log = LoggerFactory.getLogger(QiXinBaoServiceImpl.class);

    @Value("${qixin.appkey}")
    private String appkey;
    @Value("${qixin.secretKey}")
    private String secretKey;
    @Autowired
    private IEApiLogService eApiLogService;

    @Override
    public BusinessInfo getBusinessInfoByName(String name, LoginUser loginUser) {
        BusinessInfo businessInfo=new BusinessInfo();
        String baseUrl = "https://api.qixin.com/APIService/enterprise/getBasicInfo";
        EApiLog eApiLog=new EApiLog();
        eApiLog.setApiName("BUSINESSINFO");
        eApiLog.setApiUrl(baseUrl);
        eApiLog.setRequest(name);
        Map<String, String> urlParams = new HashMap<String, String>();
        // 设置url参数
        urlParams.put("appkey", appkey);
        urlParams.put("secret_key", secretKey);
        urlParams.put("keyword", name);

        List<String> concatParams = new ArrayList<String>();
        for (String s : urlParams.keySet()) {
            concatParams.add(s + "=" + urlParams.get(s));
        }

        Unirest.setTimeouts(0, 2000);
        try {
            // 发送get请求，得到响应
            HttpResponse<String> response = Unirest.get(baseUrl + "?" + String.join("&", concatParams))
                    .asString();
            businessInfo= JSON.parseObject(response.getBody(),BusinessInfo.class);
            log.info("businessInfo",response.getBody());
            if(StringUtils.isNotEmpty(businessInfo.getStatus())){
                eApiLog.setResponseCode(businessInfo.getStatus());
                eApiLog.setResponseMsg(businessInfo.getMessage());
            }
        } catch (Exception e) {
            eApiLog.setResponseCode("500");
            eApiLog.setResponseMsg("请求异常");
            e.printStackTrace();
        }
        eApiLogService.insertEApiLogSy(eApiLog,loginUser);
        return businessInfo;
    }

    @Override
    public BusinessShareholder getBusinessShareholderByCode(String code,LoginUser loginUser) {
        BusinessShareholder businessShareholder=new BusinessShareholder();
        String baseUrl = "https://api.qixin.com/APIService/enterprise/getPartners";
        EApiLog eApiLog=new EApiLog();
        eApiLog.setApiName("SHAREHOLDER");
        eApiLog.setApiUrl(baseUrl);
        eApiLog.setRequest(code);
        Map<String, String> urlParams = new HashMap<String, String>();
        // 设置url参数
        urlParams.put("appkey", appkey);
        urlParams.put("secret_key", secretKey);
        urlParams.put("keyword", code);

        List<String> concatParams = new ArrayList<String>();
        for (String s : urlParams.keySet()) {
            concatParams.add(s + "=" + urlParams.get(s));
        }

        Unirest.setTimeouts(0, 2000);
        try {
            // 发送get请求，得到响应
            HttpResponse<String> response = Unirest.get(baseUrl + "?" + String.join("&", concatParams))
                    .asString();
            businessShareholder= JSON.parseObject(response.getBody(),BusinessShareholder.class);
            log.info("businessShareholder",response.getBody());
            if(StringUtils.isNotEmpty(businessShareholder.getStatus())){
                eApiLog.setResponseCode(businessShareholder.getStatus());
                eApiLog.setResponseMsg(businessShareholder.getMessage());
            }
        } catch (Exception e) {
            eApiLog.setResponseCode("500");
            eApiLog.setResponseMsg("请求异常");
            e.printStackTrace();
        }
        eApiLogService.insertEApiLogSy(eApiLog,loginUser);
        return businessShareholder;
    }

    @Override
    public SocialInfo getSocialInfoByCode(String code,LoginUser loginUser) {
        SocialInfo socialInfo=new SocialInfo();
        String baseUrl = "https://api.qixin.com/APIService/reports/getSocialSecurityByName";
        EApiLog eApiLog=new EApiLog();
        eApiLog.setApiName("SOCIALINFO");
        eApiLog.setApiUrl(baseUrl);
        eApiLog.setRequest(code);
        Map<String, String> urlParams = new HashMap<String, String>();
        // 设置url参数
        urlParams.put("appkey", appkey);
        urlParams.put("secret_key", secretKey);
        urlParams.put("keyword", code);

        List<String> concatParams = new ArrayList<String>();
        for (String s : urlParams.keySet()) {
            concatParams.add(s + "=" + urlParams.get(s));
        }

        Unirest.setTimeouts(0, 2000);
        try {
            // 发送get请求，得到响应
            HttpResponse<String> response = Unirest.get(baseUrl + "?" + String.join("&", concatParams))
                    .asString();
            log.info("socialInfo",response.getBody());
            socialInfo= JSON.parseObject(response.getBody(),SocialInfo.class);
            if(StringUtils.isNotEmpty(socialInfo.getStatus())){
                eApiLog.setResponseCode(socialInfo.getStatus());
                eApiLog.setResponseMsg(socialInfo.getMessage());
            }
        } catch (Exception e) {
            eApiLog.setResponseCode("500");
            eApiLog.setResponseMsg("请求异常");
            e.printStackTrace();
        }
        eApiLogService.insertEApiLogSy(eApiLog,loginUser);
        return socialInfo;
    }
}
